<template>
  <WsmTable
    ref="table"
    load-url="/dbms-server/base/intercode/page"
    :change-load="true"
    v-model="queryFormType"
  >
    <template #title>{{ t('dbms.interCode.title') }}</template>
    <template #query="{ data }">
      <el-form-item prop="appName" :label="t('dbms.interCode.appName')">
        <el-select
          style="width: 200px"
          v-model="data.appName.value"
          :placeholder="t('dbms.interCode.appName')"
        >
          <el-option
            v-for="item in microservices"
            :key="item.appName"
            :label="item.appNote"
            :value="item.appName"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('dbms.interCode.code')">
        <el-input v-model="data.code.value" :placeholder="t('dbms.interCode.code')" />
      </el-form-item>
      <el-form-item :label="t('dbms.interCode.codeState')">
        <el-radio-group v-model="data.codeState.value">
          <el-radio value="">{{ t('dbms.interCode.all') }}</el-radio>
          <el-radio value="0">{{ t('dbms.interCode.create') }}</el-radio>
          <el-radio value="1">{{ t('dbms.interCode.fixed') }}</el-radio>
        </el-radio-group>
      </el-form-item>
    </template>
    <template #button="{ row }">
      <el-button type="primary" v-hasPermi="'add'" @click="addForm.show()">{{
        t('common.add')
      }}</el-button>
      <template v-if="row">
        <el-button v-hasPermi="'update'" @click="update(row.id)" type="primary">{{
          t('common.update')
        }}</el-button>
        <el-button
          v-hasPermi="'delete'"
          v-if="row.codeState == 0"
          @click="del(row.id)"
          type="danger"
          >{{ t('common.delete') }}</el-button
        >
      </template>
    </template>
    <template #column>
      <el-table-column prop="codeState" :label="t('dbms.interCode.codeState')" width="100">
        <template #default="scope">
          <template v-if="scope.row.codeState == 1">
            <el-tag effect="dark" type="danger">{{ t('dbms.interCode.fixed') }}</el-tag>
          </template>
          <template v-else>
            <el-tag effect="dark" type="success">{{ t('dbms.interCode.create') }}</el-tag>
          </template>
        </template>
      </el-table-column>
      <el-table-column prop="code" :label="t('dbms.interCode.code')" width="250" />
      <el-table-column prop="appName" :label="t('dbms.interCode.appName')" width="180" />
      <el-table-column prop="msgName" :label="t('dbms.interCode.msgName')" />
    </template>
  </WsmTable>
  <WsmForm
    ref="addForm"
    v-model="addData"
    submit-url="/dbms-server/base/intercode/add"
    label-width="100px"
    :init-data="initData"
    :title="t('dbms.interCode.addTitle')"
    @success="flushed"
  >
    <template #default="{ form }">
      <el-row>
        <el-col :span="24">
          <el-form-item prop="code" :label="t('dbms.interCode.code')">
            <el-input v-model="form.code" :placeholder="t('dbms.interCode.code')" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item prop="appName" :label="t('dbms.interCode.appName')">
            <el-select v-model="form.appName" :placeholder="t('dbms.interCode.appName')">
              <el-option
                v-for="item in microservices"
                :key="item.appName"
                :label="item.appNote"
                :value="item.appName"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="msgName" :label="t('dbms.interCode.msgName')">
            <el-input v-model="form.msgName" :placeholder="t('dbms.interCode.msgName')" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-for="lang in langs" :key="lang.lang">
        <el-col :span="24">
          <el-form-item :label="lang.langName">
            <el-input v-model="form[lang.lang]" :placeholder="lang.langName" />
          </el-form-item>
        </el-col>
      </el-row>
    </template>
  </WsmForm>

  <WsmForm
    ref="updateForm"
    v-model="updateData"
    load-url="/dbms-server/base/intercode/get"
    label-width="100px"
    submit-url="/dbms-server/base/intercode/update"
    :title="t('dbms.interCode.updateTitle')"
    @success="flushed"
  >
    <template #default="{ form }">
      <el-row>
        <el-col :span="24">
          <el-form-item prop="msgName" :label="t('dbms.interCode.msgName')">
            <el-input v-model="form.msgName" :placeholder="t('dbms.interCode.msgName')" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-for="lang in langs" :key="lang.lang">
        <el-col :span="24">
          <el-form-item :label="lang.langName">
            <el-input v-model="form[lang.lang]" :placeholder="lang.langName" />
          </el-form-item>
        </el-col>
      </el-row>
    </template>
  </WsmForm>
</template>
<script setup lang="ts">
import { WsmTable } from '@/components/WsmTable'
import { WsmForm } from '@/components/WsmForm'
import { QueryFormType, QueryType } from '@/components/WsmTable/src/types'
import { listApi } from '@/api/dbms/interlang'
import { delApi } from '@/api/dbms/intercode'
import { listApi as listMicroservices } from '@/api/dbms/microservices'
import { ref } from 'vue'
import { ElMessageBox } from 'element-plus'
import { useI18n } from '@/hooks/web/useI18n'
import { FormDataType } from '@/components/WsmForm/src/types'
import { InterLangType } from '@/api/dbms/interlang/types'

const { t } = useI18n()

//表格对象
const table = ref()

const langs = ref<InterLangType[]>([])

//新增表单对象
const addForm = ref()
const initData = ref<FormDataType>({
  codeState: '0'
})
const addData: FormDataType = {
  code: '',
  msgName: '',
  appName: '',
  codeState: '0'
}

//修改表单对象
const updateForm = ref()
const updateData: FormDataType = {
  id: '',
  code: '',
  msgName: ''
}

//查询定义
const queryFormType: QueryFormType = {
  code: {
    value: '',
    queryType: QueryType.LIKE
  },
  appName: {
    value: '',
    queryType: QueryType.EQ
  },
  codeState: {
    value: '',
    queryType: QueryType.EQ
  }
}

const loadLang = async () => {
  const rs = await listApi()
  langs.value = rs.data
  rs.data.forEach((lang: InterLangType) => {
    initData[lang.lang] = ''
    addData[lang.lang] = ''
    updateData[lang.lang] = ''
  })
}

loadLang()

const microservices = ref()

const loadMicroservice = async () => {
  const rs = await listMicroservices()
  if (rs.success) {
    microservices.value = rs.data
  }
}
loadMicroservice()

const update = (id: string) => {
  updateForm.value.show({ id })
}

const del = async (id: string) => {
  ElMessageBox.confirm(t('common.delMessage'), t('common.delWarning'), {
    confirmButtonText: t('common.ok'),
    cancelButtonText: t('common.cancel'),
    type: 'warning'
  }).then(async () => {
    const rs = await delApi({ id })
    if (rs.success) {
      table.value.load(1)
    }
  })
}

const flushed = () => {
  table.value.load(1)
}
</script>

<style scoped></style>
